/**
 * 
 */
package pl.edu.agh.to;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

/**
 * @author karulis
 * 
 */
abstract class TemplateActivity extends Activity implements Observer {
	private ListView listView;
	private Spinner spinner;
	private final DialogsHandler dialogsHandler = new DialogsHandler();
	private DBAccess dBAccess;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(getLayoutResID());
		onCreateImplementation();
		getdBAccess().addObserver(this);
	}

	protected void onCreateImplementation() {
		setupSpinner();
		// setupEmptyList();
	}

	/**
	 * @return the dBAccess
	 */
	public DBAccess getdBAccess() {
		if (dBAccess == null) {
			dBAccess = DBAccess.instance(this);

		}
		return dBAccess;
	}

	/**
	 * @return the dialogsHandler
	 */
	protected DialogsHandler getDialogsHandler() {
		return dialogsHandler;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = getDialogsHandler().dialog(id, this);
		if (dialog != null) {
			return dialog;
		} else {
			return super.onCreateDialog(id);
		}

	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		boolean consuemed = getDialogsHandler().prepareDialog(id, dialog);
		if (!consuemed) {
			super.onPrepareDialog(id, dialog);
		}
	}

	@Override
	public void update(Observable obj, Object arg) {
		switch ((DBChange) arg) {
		case AttendChange:
			attendChange();
			break;
		case RateChange:
			rateChange();
			break;
		case CourseChange:
			courseChange();
			break;
		case ParticipantChange:
			participantChange();
			break;
		case ScheduleChange:
			scheduleChange();
			break;
		default:
			break;
		}
	}

	abstract protected void scheduleChange();

	abstract protected void attendChange();

	abstract protected void rateChange();

	abstract protected void courseChange();

	abstract protected void participantChange();

	abstract int getLayoutResID();

	abstract int getSpinnerID();

	abstract int getListViewID();

	abstract List<Object> listViewContent();

	abstract List<Object> spinnerContent();

	abstract int getMenuRes();

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(getMenuRes(), menu);
		return true;
	}

	String getSpinnerSelectedItem() {
		Object sel = getSpinner().getSelectedItem();
		if (sel != null) {
			return sel.toString();
		} else {
			// TODO that might not be the best solution
			return null;
		}
	}

	/**
	 * @return the spinner
	 */
	protected Spinner getSpinner() {
		if (spinner == null) {
			spinner = (Spinner) findViewById(getSpinnerID());
		}
		return spinner;
	}

	/**
	 * @return the listView
	 */
	protected ListView getListView() {
		if (listView == null) {
			listView = (ListView) findViewById(getListViewID());
		}
		return listView;
	}

	/**
	 * 
	 */
	void onSpinnerItemSelected() {
		setListAdapter(listViewContent());
	}

	/**
	 * @param course
	 */
	protected void setListAdapter(List<Object> list) {
		getListView().setAdapter(
				new ArrayAdapter<Object>(this, R.layout.list_item, list));
	}

	/**
	 * 
	 */
	private void setupEmptyList() {
		setListAdapter(new ArrayList<Object>());
	}

	protected void setupList() {
		setListAdapter(listViewContent());
	}

	/**
	 * 
	 */
	protected void setupSpinner() {
		ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this,
				android.R.layout.simple_spinner_item, spinnerContent());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getSpinner().setAdapter(adapter);
		getSpinner().setOnItemSelectedListener(
				new OrganizerOnItemSelectedListener(this));
		setupList();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		showDialog(item.getItemId());
		return super.onOptionsItemSelected(item);
	}

}
